<?php
/**
 * @uses       Custom_Controller_Action
 * @license    Ritesh Jha (mailrkj@gmail.com)
 */

class Custom_Controller_Action extends Zend_Controller_Action
{
	/**
	 * Flash Messenger object, will be used in controller
	 * @var object
	 */
	protected $_flash = '';

	/**
	 * Contains site full url
	 * @var string
	 */
	protected $_site_url = '';

	/**
	 * Check whether the request is ajax
	 * @var bool
	 */
	protected $isAjax = false;

	/**
	 * Check whether current user is admin or not
	 * @var bool
	 */
	protected $isAdmin = false;

	/**
	 * Set search key if search is posted
	 * @var string
	 */
	protected $searchKey = '';

	/**
	 * Auth object
	 * @var object
	 */
	protected $auth = '';

	/**
	 * Config container
	 * @var object
	 */
	protected $Config = '';

	/**
	 * Logger object
	 * @var object
	 */
	protected $_logger = '';

	function preDispatch()
	{
		$this->_logger	= Zend_Registry::get('logger');
		$this->Config	= Zend_Registry::get('config');
		$this->_flash	= $this->_helper->getHelper('FlashMessenger');
		$this->_site_url = $this->getRequest()->getScheme()."://".$this->getRequest()->getHttpHost();
		$this->isAjax = ($this->getRequest()->isXmlHttpRequest())?true:false;;
		$auth = Zend_Auth::getInstance();
		$this->auth = $auth;
		$this->isAdmin = ($auth->hasIdentity() && $auth->getIdentity()->role_id == User_Model_DbTable_Role::ADMIN_USER)?true:false;

		//set search for listing
		$this->setSearch();
		$this->_setActiveMenu();

		#set to all view level variable
		$this->view->siteURL = $this->_site_url;
		$this->view->aclAuth = new User_Plugin_Auth_AuthPlugin();
		$this->view->isAdmin = $this->isAdmin;
		$this->view->dateFormat = $this->Config->params->dateFormat;
        $this->view->timeFormat = $this->Config->params->timeFormat;
        $this->view->jdateFormat = $this->Config->params->jquery->dateFormat;
        $this->view->jtimeFormat = $this->Config->params->jquery->timeFormat;
	}

	/**
	 * Add message to FlashMessenger plugin
	 * @param string $message
	 * @param string $type  Message type success,error etc
	 */
	public function addMessage($message, $type = 'success')
	{
		$this->_flash->addMessage(array($type=>__($message)));
	}

	/**
	 * Set column sort for listing pages
	 * @param object $model
	 */
	protected function setSort($model)
	{
		$sort_key = $this->_getParam('sort');
		$raw_key = $sort_key;
		
		if(method_exists($model, 'sortedFields'))
		{
			$sortFields = $model->sortedFields();
			$sort_key= (array_key_exists($sort_key, $sortFields))?$sortFields[$sort_key]:$sort_key;
		}
		else if(!in_array($sort_key,$model->info(Custom_Db_Table_Abstract::COLS)))
		{
			$sort_key = false;
		}

		# now set sort
		if($sort_key)
		{
			$dir = ($this->_getParam('dir'))?strtoupper($this->_getParam('dir')):'DESC';
			$model->setOrderBy($sort_key,$dir);

			#set order type in view
			$dir = ($dir == 'DESC')?'asc':'desc';
			$this->view->orderDir = array($raw_key=>$dir);
		}
		else
		{
			$model->setOrderBy('id','desc');
		}
	}

	/**
	 * Set column search in listing pages
	 * @param string $searchKey
	 */
	protected function setSearch($searchKey = 'search')
	{
		$context =  $this->getRequest()->getModuleName().":"
					.strtolower($this->getRequest()->getControllerName()).':'
					.strtolower($this->getRequest()->getActionName());

		$customSession = Zend_Registry::get('customSession');
      
        if (array_key_exists($searchKey, $this->getRequest()->getParams()))
        { //check search field is set either get or post
            $customSession->searchKey[$context] = $this->_getParam($searchKey);
			$this->searchKey = $customSession->searchKey[$context];
       	}
		else if(isset($customSession->searchKey[$context]))
		{
			$this->searchKey = $customSession->searchKey[$context];
		}
		else
		{
			#destroy all search session
			unset ($customSession->searchKey);
		}

		$this->view->searchKey = $this->searchKey;
	}

	private function _setActiveMenu()
	{
		if($this->isAdmin)
		{
			#selected menu
			if($this->_request->getModuleName() == 'admin')
				$this->view->activeMenu = 0;
			else if($this->_request->getModuleName() == 'service')
				$this->view->activeMenu = 1;
			else
				$this->view->activeMenu = 2;
		}
		else
		{
			if($this->_request->getModuleName() == 'service')
				$this->view->activeMenu = 0;
			else
				$this->view->activeMenu = 1;
		}
	}

	protected function RedirectUrl($url, array $options = array())
	{
		$this->_helper->redirector->gotoUrl($url, $options);
	}

	protected function Redirector($action, $controller, $module, $params = array())
	{
		$this->_helper->redirector($action, $controller, $module, $params);
	}
}

?>
